import java.sql.*;
import java.util.ResourceBundle;

public class DbUtil {

    private static  String driver;
    private static  String url;
    private static  String password;
    private static  String username;
    // 静态代码块
    static {
        // 获取相关配置
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
        driver = jdbc.getString("driver");
        url = jdbc.getString("url");
        username = jdbc.getString("name");
        password = jdbc.getString("password");
        // 注册驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 获取连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }

    // 关闭资源
    public  static void close(ResultSet rs, Statement st,Connection ct){
        if(rs !=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(st !=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(ct !=null){
            try {
                ct.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
